package com.freesky.web.manager.service.impl;

import com.freesky.common.enums.RedisKeyEnum;
import com.freesky.common.redis.IRedisTemplate;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.web.manager.service.ICacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Freesky
 */
@Service
@Slf4j
public class CacheServiceImpl implements ICacheService {

    @Autowired
    private IRedisTemplate redisTemplate;
    @Override
    public JSONResponse getHomeCache() {
        String json = "";
        try {
            json = redisTemplate.getCacheObject(RedisKeyEnum.HOME_PANEL_CONTENT.getKey(), String.class);
        } catch (Exception e) {
            log.error(e.toString());
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(json);
    }

    @Override
    public JSONResponse updateHomeCache() {

        deleteHomeRedis();
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse getRecommendCache() {
        String json = "";
        try {
            json = redisTemplate.getCacheObject(RedisKeyEnum.HOME_PANEL_RECOMMEND.getKey(), String.class);
        } catch (Exception e) {
            log.error(e.toString());
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(json);

    }

    @Override
    public JSONResponse updateRecommendCache() {
        redisTemplate.delete(RedisKeyEnum.HOME_PANEL_RECOMMEND.getKey());
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse getThankCache() {
        String json = redisTemplate.getCacheObject(RedisKeyEnum.HOME_PANEL_THANK.getKey(), String.class);
        return JSONResponseDiretor.buildSuccessJSONResponse(json);
    }

    @Override
    public JSONResponse updateThankCache() {
        redisTemplate.delete(RedisKeyEnum.HOME_PANEL_THANK.getKey());
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }


    /**
     * 同步首页缓存
     */
    public void deleteHomeRedis() {
        redisTemplate.delete(RedisKeyEnum.HOME_PANEL_CONTENT.getKey());
    }
}
